<?php
class cmsdream_query {
	public $is_wap = false;	
	public $is_search = false;
	public $is_attachment = false;
	public $is_post = false;
	public $is_page = false;
	public $is_category = false;
	public $is_tag = false;
	public $is_author = false;
	public $is_feed = false;
	public $is_date = false;
	public $client = 'web';

	private function is_wap($hash){
		$this->client = cmsdream_login::get_sc("cmsdream-client-$hash","web");
		if($this->client != 'web') return true;
		$ua = strtolower(@$_SERVER['HTTP_VIA']);
		if(stristr($ua,"wap")) return ture;
		$ua = strtolower(@$_SERVER['HTTP_USER_AGENT']);
		$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|sie-|sec|philips|panasonic|alcatel|lenovo|cldc|midp|mmp|wap|ppc|phone|symbian|mobile|ios|brew)/i";
		if($ua == '' || preg_match($uachar, $ua)) return ture;
		if (isset($_SERVER['HTTP_ACCEPT']) and (strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!==false)) {
			return true;
		}else{
			return false;
		}
	}

	public function get_template_name() {
		if($this->is_feed)				return('feed');
		if($this->is_search)			return('search');
		if($this->is_attachment)		return('attachment');
		if($this->is_post)				return('post');
		if($this->is_page)				return('page');
		if($this->is_category)			return('category');
		if($this->is_tag)				return('tag');
		if($this->is_author)			return('author');
										return('index');
	}

	public function parse_query_vars($keys, $qvars, $hash) {
		$qv = &$qvars;
		foreach ( $keys as $key ) {
			if ( !isset($qv[$key]) )
				$qv[$key] = '';
		}

		$qv['p']				=  $this->absint($qv['p']);
		$qv['name']				= trim( $qv['name'] );
		$qv['page_id']			=  $this->absint($qv['page_id']);
		$qv['year']				= $this->absint($qv['year']);
		$qv['monthnum']			= $this->absint($qv['monthnum']);
		$qv['day']				= $this->absint($qv['day']);
		$qv['paged']			= $this->absint($qv['paged']);
		$qv['cat']				= preg_replace( '|[^0-9,-]|', '', $qv['cat'] );
		$qv['pagename']			= trim( $qv['pagename'] );
		$qv['category_name']	= trim( $qv['category_name'] );
		$qv['attachment_id']	= $this->absint($qv['attachment_id']);
		
		$this->is_wap = $this->is_wap($hash);
		if ( !empty($qv['s']) ) {
			$this->is_search = true;
		}
		
		if( $qv['year'] > 0 || $qv['monthnum'] > 0 || $qv['day'] > 0 ) {
			$this->is_date = true;
		}

		if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
			$this->is_attachment = true;
		}
		
		if ( '' != $qv['name'] || $qv['p'] ) {
			$this->is_post = true;
		}
		
		if ('' != $qv['pagename'] || !empty($qv['page_id']) ) {
			$this->is_page = true;
		}
		
		if ('' != $qv['category_name'] || $qv['cat'] != '') {
			$this->is_category = true;
		}
		
		if ('' != $qv['tag'] ) {
			$this->is_tag = true;
		}

		if ( (!empty($qv['author']) && ($qv['author'] !== '0')) || $qv['author_name'] != '' ) {
			$this->is_author = true;
		}
		if ( '' != $qv['feed'] )
			$this->is_feed = true;
		return $qv;
	}
	
	private function absint( $maybeint ) {
		return abs( intval( $maybeint ) );
	}

	public function __construct() {
		
	}
	
	public function __destruct(){
	
	}
	
}


